import { Toast } from 'antd-mobile'

//* 购物车相关接口
import { shopcart_list } from "../../tools/api"; //*     将购物车列表添加到 redux
import { shopcart_clear } from "../../tools/api"; //*    删除redux购物车数组数据
import { shopcart_add } from "../../tools/api"; //*  购物车的数量加 1
import { shopcart_sub } from "../../tools/api"; //*  购物车的数量减 1


// 添加购物车列表到数组
export function push_shopping( params ) {
    return { type:'SHOP' , params }
}

// 清空购物车列表数组数据
export function delete_shoppingArr( params ) {
    return { type:'CLEARCALLECT' , params }
}

//todo  购物车的数量加 1 
export function shoppingCart_addOne( params ) {
    return { type:'CLEARCALLECT' , params }
}

//*  添加购物车列表到数组里面
export function wang_shopping_push( params ) {
    return function ( dispatch ) {
        const id = JSON.parse(localStorage.getItem('userinfo')).id;

        shopcart_list( { userid : id } ).then(
            response => { 
                dispatch( push_shopping( response.data.list ) )
            }
        )
        
    }
}

//*  清空购物车
export function myshopping_clearAll( params ) {
    return function ( dispatch ) {
        const id = JSON.parse(localStorage.getItem('userinfo')).id;

        shopcart_clear( { userid : id } ).then(
            response => { 
                if( response.data.code == 200 ) {
                    dispatch( delete_shoppingArr( ) )
                } else {
                    Toast.show({ content: '网络出错啦！' })
                }
            }
        )
    }
}

//*  购物车的数量加 1 
export function myshopping_addOne( params ) {
    return function ( dispatch ) {
        const id = JSON.parse(localStorage.getItem('userinfo')).id;

        // userid 用户id 必须传 , goodid 商品id 必须传
        shopcart_add( { userid : id , goodid : params.id} ).then(
            response => { 
                if( response.data.code == 200 ) {
                    // dispatch( shoppingCart_addOne(params) )
                    console.log(params);
                }
            }
        )
    }
}

//*  购物车的数量减 1 
export function myshopping_subOne( params ) {
    return function ( dispatch ) {
        const id = JSON.parse(localStorage.getItem('userinfo')).id;

        shopcart_sub( { userid : id , goodid : params.id } ).then(
            response => { 
                if( response.data.code == 200 ) {
                    // dispatch( shoppingCart_addOne(params) )
                    console.log(params);
                }
            }
        )
    }
}